const express = require('express');
const router = express.Router();
const utils = require('../../utils');

const filters = [
    {
        Name: '姓名',
        Type: 'String',
    },
    {
        Name: '别名',
        Type: 'String',
    },
    {
        Name: '描述',
        Type: 'String',
    },
    {
        Name: '类别',
        Type: 'String',
    },
];

// talent
router.get('/',
    (req, res, next) => {
        // query
        res.locals.filter = Object.assign(
            {}, 
            res.app.modules['input-field'].generateQueryFilter(filters, req.query), 
            res.locals.filter, 
            utils.makeQueryFilters(req, 'talent'),
            { Status: '1', Deleted: false, Freeze: { $exists: false, $ne: true } },
            { 公开: true },
        );

        res.locals.fields = [
            'id',
            'LastUpdateDate',
            '标题',
            '时间',
            '姓名',
            '别名',
            '类别',
            '图片',
            '单位',
            '职位',
            '联系方式',
            '领域',
            '行业',
            '描述',
        ];
        res.locals.options = {
            sort: { 推荐指数: 'desc', 时间: 'desc', LastUpdateDate: 'desc' },
            lean: true
        };

        return next();
    },
    router.FindDocuments('talent', false, async (req, res) => {
        if(!res.locals.data || !res.locals.data.total) return;

        for (let i = 0; i < res.locals.data.docs.length; i += 1) {
            const doc = res.locals.data.docs[i];

            doc.描述 = utils.repalceHtmlToText(doc.描述 || '');
            doc.日期 = doc.时间 || doc.LastUpdateDate;
            delete doc.LastUpdateDate;
            delete doc.时间;

            await utils.getDictLabel(req, res, '类别', '人才类别');
            await utils.getDictLabel(req, res, '行业', '行业列表');
            await utils.getDictLabel(req, res, '领域', '领域列表', true);
        }
    })
);

// talent details
router.get('/:id',
    (req, res, next) => {
        res.locals.filter = { id: req.params.id, Status: '1', Deleted: false, Freeze: { $exists: false, $ne: true }, 公开: true };
        res.locals.fields = [
            'id',
            'LastUpdateDate',
            '推荐指数',
            '时间',
            
            '姓名',
            '别名',
            '类别',
            '图片',
            '单位',
            '职位',
            '联系方式',
            '领域',
            '行业',
            '描述',
            '附件',
            '公开',
        ];
        res.locals.options = {
            lean: true
        };

        return next();
    },
    router.FindDocuments('talent', false, async (req, res) => {
        if(res.locals.data && res.locals.data.total) {
            res.locals.data = res.locals.data.docs[0];
            res.locals.data.日期 = res.locals.data.时间 || res.locals.data.LastUpdateDate;

            await utils.getDictLabel(req, res, '类别', '人才类别');
            await utils.getDictLabel(req, res, '行业', '行业列表');
            await utils.getDictLabel(req, res, '领域', '领域列表', true);
        } else {
            res.locals.data = {};
        }
    }),
);

// related
router.get('/related/:id',
    (req, res, next) => {
        // query
        res.locals.filter = Object.assign(
            {}, 
            res.app.modules['input-field'].generateQueryFilter(filters, req.query), 
            res.locals.filter, 
            utils.makeQueryFilters(req, 'talent'),
            { Status: '1', Deleted: false, Freeze: { $exists: false, $ne: true } },
            { 公开: true },
            { id: { $ne: req.params.id }},
        );

        res.locals.fields = [
            'id',
            'LastUpdateDate',
            '标题',
            '时间',
            '姓名',
            '别名',
            '图片',
            '描述',
            '类别',
            '行业',
            '领域',
        ];
        res.locals.options = {
            sort: { 推荐指数: 'desc', 时间: 'desc', LastUpdateDate: 'desc' },
            lean: true
        };

        return next();
    },
    router.FindDocuments('talent', false, async (req, res) => {
        if(!res.locals.data || !res.locals.data.total) return;

        for (let i = 0; i < res.locals.data.docs.length; i += 1) {
            const doc = res.locals.data.docs[i];

            doc.描述 = utils.repalceHtmlToText(doc.描述 || '');
            doc.日期 = doc.时间 || doc.LastUpdateDate;
            delete doc.LastUpdateDate;
            delete doc.时间;

            await utils.getDictLabel(req, res, '类别', '人才类别');
            await utils.getDictLabel(req, res, '行业', '行业列表');
            await utils.getDictLabel(req, res, '领域', '领域列表', true);
        }
    })
);

router.get('/dict/fields', async (req, res, next) => {
    const locale = req.body.locale || req.query.locale;

    const systemConf = res.app.modules['system-config'];
    let fieldsOptions = req.query.bottom ? [] : await systemConf.get('技术领域筛选项', '平台设置');
    fieldsOptions = fieldsOptions['人才领域'] || '';
    fieldsOptions = fieldsOptions.split(',');

    const ret = [];

    for (let i = 0; i < fieldsOptions.length; i += 1) {
        const opt = fieldsOptions[i];
        if (!opt) continue;
        
        ret[i] = {
            value: opt,
            label: opt,
        };

        const theDict = await res.app.models.dictionary.findOne({Name : '领域列表', Value: opt}, {Labels: 1}).lean();

        if (theDict && theDict.Labels && theDict.Labels.length) {
            const theLabel = theDict.Labels.find((dl) => dl.Locale === locale);

            if (theLabel && theLabel.Label) {
                ret[i].label = theLabel.Label;
            }
        }
    }
    
    res.addData({
        fields: ret,
    });

    return next();
});

module.exports = router;
